#include "solar/trunk/NVideoDockWidget.h"

NVideoWidget::NVideoWidget(QWidget*):isDoubleFlag(false){
    setMouseTracking(true);
}

void NVideoWidget::mouseMoveEvent(QMouseEvent*){
    emit msev_mv("MOVE");
}


void NVideoWidget::mouseReleaseEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton){
        emit msev_rls(event);
    }
}


void NVideoWidget::enterEvent(QEnterEvent*){
    emit msev_hver("ENTER");
}

//双击全屏时，鼠标会离开组件，但本质是鼠标并未离开组件，是组件放大造成的暂时离开，因此当存在双击事件时，当作enter事件处理，并恢复双击标识
//主要目的为避免错误的发送LEAVE标识，导致底部组件隐藏定时器停止，造成意外悬停
void NVideoWidget::leaveEvent(QEvent*){
    if (isDoubleFlag)
    {
        emit msev_hver("ENTER");
        emit isDoubleFlag = false;
    } else {
        emit msev_hver("LEAVE");
    }
}


void NVideoWidget::wheelEvent(QWheelEvent *event){
    int Consult = event->angleDelta().y();
    if (Consult > 0){
        emit msev_wheel(true);
    } else {
        emit msev_wheel(false);
    }
}


void NVideoWidget::mouseDoubleClickEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton){
        emit msev_db(event);
        isDoubleFlag = true;
    }
}


void NVideoWidget::keyReleaseEvent(QKeyEvent *event){
    emit keyv_pre(event);
}


NVideoWidget::~NVideoWidget(){}


NVideoView::NVideoView(QWidget *parent){
    setContentsMargins(0, 0, 0, 0);
    setCursor(Qt::PointingHandCursor);
    setAttribute(Qt::WA_DeleteOnClose);
    setParent(parent);

    setAttribute(Qt::WA_TranslucentBackground, false);
    setAttribute(Qt::WA_NoSystemBackground, false);
    d_margin_radius(QMargins(0,0,0,0),3);
    d_filler_enable(NTitleBar::BACKCOLOR, true); //可选背景色和背景图两个模式
    d_filler_color(QColor(95,136,105,200)); //背景色
    d_floating_pixmap(":/PIXMAP/bench/music-floating.jpg"); //悬浮窗背景图
    captionBar()->d_margin_radius(QMargins(1,0,1,1),3);
    captionBar()->d_filler_enable(NTitleBar::BACKCOLOR, true);
    captionBar()->d_filler_color(QColor(251,252,253));
    captionBar()->d_caption("", QFont("PMingLiu", 12), QColor(120,120,120)); //设置标题栏文字
    captionBar()->d_filler_enable(NTitleBar::BACKCOLOR, true); //设置标题栏填充为背景色模式
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::NORWIN, 4, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::MINWIN, 4, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::MAXWIN, 4, qRgb(49,49,49));
    captionBar()->d_basicbuttom_freepixmap(NTitleBar::QUITE,  4, qRgb(49,49,49));
    captionBar()->d_basicbuttom_configur(NT::PIXLOAD_IGNORERATIO_LOWER, NT::DRAWTYPE_RECT, 8, 1.1);
    captionBar()->d_basicbuttom_where({NTitleBar::Rroof_Normal,
                                       NTitleBar::Rroof_Min,
                                       NTitleBar::Rroof_Max,
                                       NTitleBar::Rroof_Exit},
                                      {QString(MSEVKEY.WIN_NORMAL),
                                       QString(MSEVKEY.WIN_LEAVE),
                                       QString(MSEVKEY.WIN_PRESS),
                                       QString(MSEVKEY.WIN_RELEASE)});
    captionBar()->d_basicbuttom_where(NTitleBar::BasicButtom::Rroof_Normal, false);
    captionBar()->piazze()->setCentralWidget(mtpl_tracksname);

    color_adjustedmenu->adjustSize();
    color_adjustedmenu->addAction(coloradjust);

    bottom_assistwidget->doconfigur_direction(NT::ORIEN_HORIZONTAL);
    bottom_assistwidget->setFixedHeight(110);
    bottom_assistwidget->doconfigur_secolor(QColor(255,255,255,50));
    bottom_assistwidget->setVisible(false);

    mtpl_durslider->d_text_visible(false);
    mtpl_durslider->d_linear_addcolor({QColor(240,243,246)});
    mtpl_durslider->d_linear_subcolor({QColor(95,136,105)});
    mtpl_durslider->d_graph_variant(":/PIXMAP/bench/slider-corner.png", 1);

    mtpl_medialast->setFixedSize(27, 20);
    mtpl_mediastat->setFixedSize(35, 35);
    mtpl_medianext->setFixedSize(27, 20);
    mtpl_tracksname->setFixedSize(450,30);
    mtpl_begindra->setFixedSize(100, 25);
    mtpl_durslider->setFixedHeight(25);
    mtpl_finishdra->setFixedSize(100, 25);

    mtpl_tracksname->setAttribute(Qt::WA_TranslucentBackground);

    mtpl_tracksname->setFont(QFont("KaiTi_GB2312", 15));
    mtpl_tracksname->setPenColor(QColor(95,136,105));
    mtpl_tracksname->setVisible(false);
    mtpl_begindra->doconfigur_color(QColor(240,243,246));
    mtpl_begindra->doconfigur_textsetting(Qt::AlignCenter);
    mtpl_finishdra->doconfigur_color(QColor(240,243,246));
    mtpl_finishdra->doconfigur_textsetting(Qt::AlignCenter);

    MT_centraroof_Hbox->addWidget(mtpl_begindra, 0, Qt::AlignVCenter | Qt::AlignLeft);
    MT_centraroof_Hbox->addWidget(mtpl_durslider, 1, Qt::AlignVCenter);
    MT_centraroof_Hbox->addWidget(mtpl_finishdra, 0, Qt::AlignVCenter | Qt::AlignRight);
    MT_centraroof_Hbox->setSpacing(7);

    MT_centramax_Hbox->addWidget(mtpl_medialast);
    MT_centramax_Hbox->addWidget(mtpl_mediastat);
    MT_centramax_Hbox->addWidget(mtpl_medianext);
    MT_centramax_Hbox->setContentsMargins(0, 0, 0, 0);
    MT_centramax_Hbox->setSpacing(10);
    bottom_assistgeid->addLayout(MT_centraroof_Hbox, 0, 0, 2, 20);
    bottom_assistgeid->addLayout(MT_centramax_Hbox, 1, 8, 2, 4);
    bottom_assistgeid->setContentsMargins(0, 0, 0, 0);
    bottom_assistgeid->setVerticalSpacing(25);


    //总布局包含视频窗口组件和底部弹出式窗口组件
    inner_grid->addWidget(video_widget, 0, 0, 20, 20);
    inner_grid->addWidget(bottom_assistwidget, 19, 0, 1, 20, Qt::AlignBottom);
    inner_grid->setContentsMargins(0, 0, 0, 0);
    //放置一个空组件，去掉顶部边框，注意要使用窗口类如widget，mainwindown，其他组件如toolbar也可以去掉边框但是会占用空间
    mtpl_durslider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    mtpl_tracksname->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    setCentralWidget(inner_widget);

    mtpl_begindra->doconfigur_text("4:01:782");
    mtpl_tracksname->setText("我爱你梁梦雨");
    mtpl_finishdra->doconfigur_text("4:11:128");

    connect(color_adjustedmenu, &QMenu::triggered, this, &NVideoView::popuMenuCheck);
    connect(video_widget, &NVideoWidget::msev_mv, this, &NVideoView::mouseMoveDispose);
    connect(video_widget, &NVideoWidget::msev_hver, this, &NVideoView::mouseMoveDispose);

    connect(video_widget, &NVideoWidget::msev_db, this, &NVideoView::mouseDoubleClickEvent);
    connect(video_widget, &NVideoWidget::msev_rls, this, &NVideoView::mouseReleaseEvent);
    connect(video_widget, &NVideoWidget::keyv_pre, this, &NVideoView::keyReleaseEvent);
    connect(&HIDER_BUTTONMD_TIMER, &QTimer::timeout, this, &NVideoView::mouseMoveConsequence);

    connect(brightness_slider, static_cast<void (NSlider:: *)(double)>(&NSlider::slidervalue), brightness_slider,
           [this](int value){setColorCondition(DurationColor::brightness, value);});
    connect(contrast_slider, static_cast<void (NSlider::*)(double)>(&NSlider::slidervalue), contrast_slider,
           [this](int value){setColorCondition(DurationColor::Contrast, value);});
    connect(hue_slider, static_cast<void (NSlider::*)(double)>(&NSlider::slidervalue), hue_slider,
           [this](int value){setColorCondition(DurationColor::Hue, value);});
    connect(saturation_slider, static_cast<void (NSlider::*)(double)>(&NSlider::slidervalue), saturation_slider,
           [this](int value){setColorCondition(DurationColor::Saturation, value);});

}

void NVideoView::mouseMoveDispose(QString movename){
    if (property("max-screen").toBool())
    {
        if (movename == "LEAVE")
        {
            if (HIDER_BUTTONMD_TIMER.isActive()) HIDER_BUTTONMD_TIMER.stop();
        }
        else
        {

            HIDER_BUTTONMD_TIMER.start(3000);
        }
    }
    if (!mtpl_tracksname->isVisible()) mtpl_tracksname->setVisible(true);
    if (!bottom_assistwidget->isVisible()) bottom_assistwidget->setVisible(true);
    if (video_widget->cursor() == QCursor(Qt::BlankCursor)) video_widget->setCursor(Qt::PointingHandCursor);
}


void NVideoView::mouseMoveConsequence(){
    bottom_assistwidget->setVisible(false);
    mtpl_tracksname->setVisible(false);
    video_widget->setCursor(Qt::BlankCursor);
}


void NVideoView::mouseReleaseEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton) emit msev_rls();
}


void NVideoView::mouseDoubleClickEvent(QMouseEvent *event){
    if (event->button() == Qt::LeftButton)
    {
        mouseDoubleAssert();
    }
}


void NVideoView::mouseMoveEvent(QMouseEvent*){
    mouseMoveDispose("ENTER");
}



void NVideoView::mouseDoubleAssert(){
    if (property("max-screen").toBool())
    {
        setGeometry(property("max-ago-size").toRect());
        mtpl_tracksname->setVisible(false);
        setProperty("max-screen", false);
    } else {
        setProperty("max-ago-size", geometry());
        setGeometry(QApplication::primaryScreen()->geometry());
        mtpl_tracksname->setVisible(true);
        setProperty("max-screen", true);
    }
    emit msev_db();
}


void NVideoView::keyReleaseEvent(QKeyEvent *Event){
    if (Event->key() == Qt::Key_Escape)
    {
        if (property("max-screen").toBool()) mouseDoubleAssert();
    }
}


void NVideoView::contextMenuEvent(QContextMenuEvent *event){
    NMainWindow::contextMenuEvent(event);
    color_adjustedmenu->popup(mapToGlobal(event->pos()));
}


void NVideoView::popuMenuCheck(QAction *action){
    if (action->text().count("ColorAdjust")){
        // Color Conditioning widget
        color_adjustwidget->doconfigur_secolor(GradientColor);
        color_adjustwidget->setWindowFlags(Qt::FramelessWindowHint);
        color_adjustwidget->setFixedSize(450, 110);

        // brightness_slider->setValue(video_widget->brightness());
        // contrast_slider->setValue(video_widget->contrast());
        // hue_slider->setValue(video_widget->hue());
        // saturation_slider->setValue(video_widget->saturation());

        brightness->setFixedSize(120, 25);
        contrast->setFixedSize(120, 25);
        hue->setFixedSize(120, 25);
        saturation->setFixedSize(120, 25);
        brightness_slider->setFixedSize(240, 25);
        contrast->setFixedSize(240, 25);
        hue->setFixedSize(240, 25);
        saturation_slider->setFixedSize(240, 25);

        brightness->doconfigur_textsetting(Qt::AlignCenter, QFont("KaiTi_GB2312", 12));
        contrast->doconfigur_textsetting(Qt::AlignCenter, QFont("KaiTi_GB2312", 12));
        hue->doconfigur_textsetting(Qt::AlignCenter, QFont("KaiTi_GB2312", 12));
        saturation->doconfigur_textsetting(Qt::AlignCenter, QFont("KaiTi_GB2312", 12));

        brightness->doconfigur_format("Brightness %1");
        contrast->doconfigur_format("Contrast %1");
        hue->doconfigur_format("Hue %1");
        saturation->doconfigur_format("Saturation %1");

        // slider - property config
        brightness_slider->setRange(0, 30);
        contrast_slider->setRange(0, 30);
        hue_slider->setRange(0, 30);
        saturation_slider->setRange(0, 30);

        ColorAdjustlayout->addRow(brightness, brightness_slider);
        ColorAdjustlayout->addRow(contrast, contrast_slider);
        ColorAdjustlayout->addRow(hue, hue_slider);
        ColorAdjustlayout->addRow(saturation, saturation_slider);
        ColorAdjustlayout->setVerticalSpacing(5);
        ColorAdjustlayout->setContentsMargins(10, 5, 2, 5);

        QPoint map_global_pos = mapToGlobal(video_widget->pos());
        color_adjustwidget->setVisible(true);
        color_adjustwidget->move(map_global_pos);
    }
}


void NVideoView::setColorCondition(DurationColor logo, int value){
    // if (logo == DurationColor::brightness) {
    //     video_widget->setBrightness(value);
    //     brightness->doconfigur_formattext(QString::number(value));
    //     brightness_slider->setValue(value);
    // } else if (logo == DurationColor::Contrast) {
    //     video_widget->setContrast(value);
    //     contrast->doconfigur_formattext(QString::number(value));
    //     contrast_slider->setValue(value);
    // } else if (logo == DurationColor::Hue) {
    //     video_widget->setHue(value);
    //     hue->doconfigur_formattext(QString::number(value));
    //     hue_slider->setValue(value);
    // } else if (logo == DurationColor::Saturation) {
    //     video_widget->setSaturation(value);
    //     saturation->doconfigur_formattext(QString::number(value));
    //     saturation_slider->setValue(value);
    // }
}


NVideoView::~NVideoView(){
    delete total_temporary;

}
